package com.hex.liyue.usermodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.storemodule.activity.PaymentOrdersActivity;
import com.hex.liyue.usermodule.bean.OrderDetailsResultBean;
import com.hex.liyue.usermodule.presenter.IOrderDetailsView;
import com.hex.liyue.usermodule.presenter.OrderDetailsPresenter;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/29.
 */
public class OrderDetailsActivity extends BaseActivity implements IOrderDetailsView {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;

    @Bind(R.id.tv_order_time)
    TextView tvOrderTime;
    @Bind(R.id.iv_order_icon)
    ImageView ivOrderIcon;
    @Bind(R.id.tv_order_state)
    TextView tvOrderState;
    @Bind(R.id.tv_order_id)
    TextView tvOrderId;
    @Bind(R.id.tv_order_price)
    TextView tvOrderPrice;
    @Bind(R.id.iv_order_gift_icon)
    ImageView ivOrderGiftIcon;
    @Bind(R.id.tv_order_gift_brand)
    TextView tvOrderGiftBrand;
    @Bind(R.id.tv_order_gift_name)
    TextView tvOrderGiftName;
    @Bind(R.id.tv_order_gift_num)
    TextView tvOrderGiftNum;
    @Bind(R.id.tv_oeder_gift_price)
    TextView tvOederGiftPrice;
    @Bind(R.id.rl_operate_box)
    RelativeLayout rlOperateBox;

    private OrderDetailsPresenter orderDetailsPresenter;
    private String order_id;

    @Override
    protected void bindPresenter() {
        orderDetailsPresenter = new OrderDetailsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        order_id = getIntent().getStringExtra("orderId");
        L.d("WJ", "order_id=" + order_id);
        orderDetailsPresenter.getOrderDetails(order_id);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_order_details;
    }

    @Override
    protected void initToolbar() {
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("订单详情");
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_cancel_order, R.id.tv_go_to_pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.tv_cancel_order:
                //取消订单
                orderDetailsPresenter.cancelOrder(order_id);
                break;
            case R.id.tv_go_to_pay:
                //去支付
                Intent intent = new Intent(this, PaymentOrdersActivity.class);
                intent.putExtra("order_id", order_id);
                intent.putExtra("isOtherRun", true);
                startActivityForResult(intent, 0);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            //支付完成回来了 刷新订单详情
            orderDetailsPresenter.getOrderDetails(order_id);
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void setInfo(OrderDetailsResultBean.DataBean dataBean) {
        tvOrderTime.setText("下单时间:" + DateUtils.getFormatDate(dataBean.create_time * 1000, "yyyy-MM-dd HH:mm:ss"));
        Glide.with(this)
                .load(dataBean.gift.gift_url)
                .error(R.drawable.no_icon)
                .into(ivOrderIcon);

        rlOperateBox.setVisibility(View.GONE);
        if (dataBean.status.equals("-1")) {
            tvOrderState.setText("订单状态：已取消");
        } else if (dataBean.status.equals("1")) {
            rlOperateBox.setVisibility(View.VISIBLE);
            tvOrderState.setText("订单状态：未支付");
        } else if (dataBean.status.equals("2")) {
            tvOrderState.setText("订单状态：已支付");
        } else if (dataBean.status.equals("3")) {
            tvOrderState.setText("订单状态：已过期");
        }
        tvOrderId.setText("订单编号：" + dataBean.order_sn);
        tvOrderPrice.setText("订单金额：" + dataBean.pay_money);
        Glide.with(this)
                .load(dataBean.gift.gift_url)
                .error(R.drawable.no_icon)
                .into(ivOrderGiftIcon);
        tvOrderGiftBrand.setText(dataBean.gift.gift_brand);
        tvOrderGiftName.setText(dataBean.gift.gift_name);
        tvOrderGiftNum.setText("数量:x " + dataBean.gift_num);
        tvOederGiftPrice.setText("单价:￥" + dataBean.gift.price);
    }
}
